package com.azier.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

@Order(10)
@Configuration
@MapperScan(
        // 此处的mapper 可以有多个
        basePackages = { BaseDataSourceConfig.MAPPER_PACKAGE },
        sqlSessionFactoryRef = "baseSqlSessionTemplate"
)
public class BaseDataSourceConfig {

    // mapper 的xml存放路径
    protected final static String MAPPER_XML_AREA = "classpath*:com/azier/**/*Mapper.xml";
    // mapper.java 存放路径，被@MapperScan扫描的，注入 sqlsession的
    protected final static String MAPPER_PACKAGE = "com.azier.**.mapper";

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.driverClass}")
    private String driverClass;


    /**
     * 注入 datasource
     * @return
     */
    @Bean(name = "baseDataSource")
    @ConfigurationProperties( prefix = "spring.datasource.base")
    @Primary
    public DataSource setDataSource()  {
        // 使用druid 则这样注入 dataSource，不需要则直接 DataSourceBuilder.create().build()
        DataSource dataSource = DataSourceBuilder.create()
                .driverClassName(driverClass)
                .password(password)
                .url(url)
                .username(user)
                .build();
        return dataSource;
    }

    /**
     * 注入 事务，在 serviceImpl 的时候使用
     * @return
     */
    @Bean(name = "baseTransationManager")
    @Primary
    public DataSourceTransactionManager setTransactionManager(){
        // 传入 dataSource
        return new DataSourceTransactionManager( setDataSource() );
    }

    /**
     * 注入 sqlSession
     * @return
     * @throws Exception
     */
    @Bean(name = "baseSqlSessionTemplate")
    @Primary
    public SqlSessionFactory setSqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(setDataSource());
        // 设置mapper.xml 扫描路径
        bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(BaseDataSourceConfig.MAPPER_XML_AREA) );
        return bean.getObject();
    }

}